package com.lmscn.lms.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.lmscn.lms.api.interceptor.ArticleKeyCondition;
import com.lmscn.lms.api.model.Article;
import com.lmscn.lms.api.model.ArticleCategory;
import com.lmscn.lms.api.model.ArticleCommentCkGood;
import com.lmscn.lms.api.model.ArticleStudentStatus;
import com.lmscn.lms.api.model.CategoryColum;
import com.lmscn.lms.api.model.CenterCourseBase;
import com.lmscn.lms.api.model.Comment;
import com.lmscn.lms.api.model.CourseBase;
import com.lmscn.lms.api.model.CourseCommentCkGood;
import com.lmscn.lms.api.model.CourseLesson;
import com.lmscn.lms.api.model.MyCommentVO;
import com.lmscn.lms.api.model.Push;
import com.lmscn.lms.api.model.PushStudent;
import com.lmscn.lms.api.model.PushVO;
import com.lmscn.lms.api.model.Student;
import com.lmscn.lms.api.model.Tag;
import com.lmscn.lms.api.model.WelcomPages;
import com.lmscn.lms.api.util.CommonUtil;
import com.lmscn.lms.api.util.DatahandleUtil;
import com.lmscn.lms.api.util.JsonKit;
import com.lmscn.lms.commons.GlobalConst;

/**
 * 书香辽宁新增接口专用控制器、
 * 
 * 更迭为国恒智林新版控制器
 * 
 * @author zhang.xx
 * @date 2016年12月20日11:07:21
 * 
 */
public class SxlnNewController extends Controller {

	public ArticleKeyCondition kcd;

	/**
	 * demo
	 */
	public void demoController() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long courseId = getParaToLong("courseId");
			Long studentId = getParaToLong("studentId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");

			dataMap.put("error_code", "0");
			dataMap.put("msg", "成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 我的评论接口
	 * 
	 * 待完善，资讯的评论功能
	 * 
	 * @author zhang.xx
	 * @date 2016年12月20日15:01:52
	 */
	public void myCommits() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			Long studentId = getParaToLong("studentId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");

			Student student = Student.dao.findById(studentId);

			// 替换查询条件为user_id 2016年10月14日14:57:28
			Page<Comment> commentPage = Comment.dao.findAllCommentPageByStuId(pageNumber, pageSize, studentId);
			List<Comment> commentList = commentPage.getList();

			List<MyCommentVO> myCommentVOs = new ArrayList<MyCommentVO>();

			for (int i = 0; i < commentList.size(); i++) {
				MyCommentVO myCommentVO = new MyCommentVO();
				Comment c = commentList.get(i);

				Long commentId = c.getLong("id");
				Long targetId = c.getLong("target_id");
				String targetType = c.getStr("target_type");

				if ("course".equals(targetType)) {
					CourseBase courseBase = CourseBase.dao.findById(targetId);
					courseBase.set("url", CommonUtil.getMediaUrl(courseBase));
					courseBase.set("small_img", courseBase.getStr("small_img"));
					courseBase.set("large_img", courseBase.getStr("small_img"));

					// 课程类型的设置
					CourseLesson courseLesson = courseBase.getCourseLesson();
					courseBase.set("assemble_type", courseLesson.getStr("assemble_type"));

					// 评论被点赞数
					long ckGoodNum = CourseCommentCkGood.dao.getCommentCkGoodNumByID(commentId);
					myCommentVO.setCkGoodNum(ckGoodNum);

					myCommentVO.setCourseBase(courseBase);
					myCommentVO.setArticle(null);

				} else if ("article".equals(targetType)) {
					Article a = Article.dao.getSimpleArticleById(targetId);

					String img = a.getStr("img");
					if (img != null && !"".equals(img)) {
						img = CommonUtil.getArticleHeadUrl(img);
					}
					a.set("img", img);

					// 评论被点赞数
					long ckGoodNum = ArticleCommentCkGood.dao.getCommentCkGoodNumByID(commentId);
					myCommentVO.setCkGoodNum(ckGoodNum);

					myCommentVO.setCourseBase(null);
					myCommentVO.setArticle(a);

				}

				myCommentVO.setComment(c);

				myCommentVOs.add(myCommentVO);
			}

			data.put("myCommentList", myCommentVOs);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取我的评论成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取我的评论失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/* 接口代码初始化：2016年11月11日10:42:00 */
	/**
	 * 首页类别板块接口
	 * 
	 * 2016年11月12日10:18:38
	 */
	public void categoryColumn() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {
			List<CategoryColum> categoryColums = CategoryColum.dao.getCategoryColums();

			for (int i = 0; i < categoryColums.size(); i++) {
				// 补全图片路径
				CategoryColum w = categoryColums.get(i);
				String img = CommonUtil.getCategoryColumimgUrl(w);
				w.set("img", img);

				String columName = w.getStr("name");
				Long category_id;
				// 根据分类名称查询相应的分类ID
				ArticleCategory ac = ArticleCategory.dao.getArticleCategoryByName(columName);
				if (ac != null) {
					category_id = ac.getLong("id");
					w.set("category_id", category_id);
				}
			}

			data.put("categoryColums", categoryColums);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 首页猜你喜欢接口
	 * 
	 * @date 2016年11月12日10:57:45 完成假数据展示接口
	 */
	public void guessYouLike() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");
			// Long courseId = getParaToLong("courseId");
			// int pageNumber = getParaToInt("pageNumber");
			// int pageSize = getParaToInt("pageSize");

			List<CourseBase> courseBases = CourseBase.dao.getGeussLikeCoursesBases();

			List<CenterCourseBase> likeCourseBases = DatahandleUtil.handleCourseList(courseBases);

			data.put("likeCourseBases", likeCourseBases);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}
	
	
	public void homeCourses() {
		// 猜你喜欢 
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		Long studentId = getParaToLong("studentId");
		
		try {
//			// 3 最新课
//			Page<CourseBase> pageCourse = CourseBase.dao.getCoursePage(1, 4, "t.create_time DESC");
//			List<CenterCourseBase> newCourseBases = DatahandleUtil.handleCourseList(pageCourse.getList());
//			dataMap.put("newCourses", newCourseBases);
//			
//			// 2猜你喜欢
//			List<CourseBase> courseBases = CourseBase.dao.getGeussLikeCoursesBases();
//			List<CenterCourseBase> likeCourseBases = DatahandleUtil.handleCourseList(courseBases);
//			dataMap.put("likeCourses", likeCourseBases);
			
			// 1 精品课
			List<CourseBase> competitiveCourse = CourseBase.dao.getFourCompetitiveCourses();
			List<CenterCourseBase> competitiveCourses = DatahandleUtil.handleCourseList(competitiveCourse);
			data.put("goodCourses", competitiveCourses);

			// 2 观看记录
			List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();
			List<CourseBase> courseBaseList = CourseBase.dao.findLastLearnedCourseList(studentId, 4);
			for (CourseBase courseBase : courseBaseList) {
				Map<String, Object> courseMap = new HashMap<String, Object>();
				courseBase.set("small_img", courseBase.getStr("small_img"));
				courseBase.set("large_img", courseBase.getStr("small_img"));
				courseMap.put("courseBase", courseBase);
				courseMap.put("courseLesson", courseBase.getCourseLesson());
				courseMap.put("courseLearningSetting", courseBase.getCourseLearningSetting());
				dataMapList.add(courseMap);
			}
			dataMap.put("lastLearnedCourses", dataMapList);
			
			
			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}



	/**
	 * 首页底部列表接口
	 * 
	 * @date 2016年11月13日16:41:17 初始版本完成；
	 */
	public void topBottomOld() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		List resultList = new ArrayList();

		try {

			// 预留的学员ID参数，特别推送应该会用到；
			Long studentId = getParaToLong("studentId");

			/**
			 * 列表分类： （1）特别推送（最新的推送的课程4个）； （2）精品微课（最新课程4门）；
			 * （3）中央精神；（4）地方动态；（5）专家解读；（6）热点词条； 3、4是资讯，5、6是文章，但是后台都是用的article表；
			 */

			// （1）特别推送 推送未做，暂时使用模拟数据
			// List<CourseBase> pushCourseBases =
			// CourseBase.dao.getFourSpecialPushCourses();
			// List<CenterCourseBase> specialPushCourses =
			// DatahandleUtil.handleCourseList(pushCourseBases);
			// 特别推送的逻辑
			List<PushStudent> pStudents = PushStudent.dao.getPushStuListByStuId(studentId);
			List pushIdList = new ArrayList();
			if (pStudents != null && pStudents.size() > 0) {
				for (int i = 0; i < pStudents.size(); i++) {
					long pushId = pStudents.get(i).getLong("push_id");
					pushIdList.add(pushId);
				}
			}

			List<CourseBase> courseBases = new ArrayList<CourseBase>();
			if (pushIdList.size() > 0) {
				for (int i = 0; i < pushIdList.size(); i++) {
					long pushId = (Long) pushIdList.get(i);
					Push push = Push.dao.findById(pushId);

					String courseIds = push.getStr("course_ids");
					if (courseIds != null && !"".equals(courseIds)) {

						String[] coursesIdArr = courseIds.split(",");
						for (int j = 0; j < coursesIdArr.length; j++) {
							CourseBase courseBase = CourseBase.dao.findById(coursesIdArr[j]);
							courseBases.add(courseBase);
						}
						if (courseBases.size() > 4) {
							// 截取前4个
							courseBases = courseBases.subList(0, 4);
							break;
						}
					}

				}
			}
			List<CenterCourseBase> specialPushCourses = DatahandleUtil.handleCourseList(courseBases);

			// （2）精品微课
			List<CourseBase> competitiveCourse = CourseBase.dao.getFourCompetitiveCourses();
			List<CenterCourseBase> competitiveCourses = DatahandleUtil.handleCourseList(competitiveCourse);

			// (3)中央精神
			long categoryId = 1;
			int articleType = 0;
			List<Article> centralSpirits = Article.dao.getFourFirstPageArticles(articleType, categoryId, 2);

			// (4)地方动态
			categoryId = 2;
			articleType = 0;
			List<Article> localDynamics = Article.dao.getFourFirstPageArticles(articleType, categoryId, 2);

			// 对3、4进行图片处理 2016年11月30日15:31:21
			for (int i = 0; i < centralSpirits.size(); i++) {
				Article a = centralSpirits.get(i);
				String img = a.getStr("img");
				if (img != null && !"".equals(img)) {
					img = CommonUtil.getArticleHeadUrl(img);
				}
				a.set("img", img);
			}
			for (int i = 0; i < localDynamics.size(); i++) {
				Article a = localDynamics.get(i);
				String img = a.getStr("img");
				if (img != null && !"".equals(img)) {
					img = CommonUtil.getArticleHeadUrl(img);
				}
				a.set("img", img);
			}

			// (5)专家解读
			categoryId = 3;
			articleType = 1;
			List<Article> expertInterpretations = Article.dao.getFourFirstPageArticles(articleType, categoryId, 3);

			// (6)热点词条
			categoryId = 4;
			articleType = 1;
			List<Article> hotWords = Article.dao.getFourFirstPageArticles(articleType, categoryId, 3);

			// 与上面的分类列表联动，顺序一致；
			List<CategoryColum> categoryColums = CategoryColum.dao.getCategoryColums();
			for (int i = 0; i < categoryColums.size(); i++) {
				long columId = categoryColums.get(i).getLong("id");
				String columName = categoryColums.get(i).getStr("name");
				int columSeq = categoryColums.get(i).getInt("seq");

				// 根据分类名称查询相应的分类ID
				Long category_id;
				ArticleCategory ac = ArticleCategory.dao.getArticleCategoryByName(columName);
				if (ac != null) {
					category_id = ac.getLong("id");
					categoryColums.get(i).set("category_id", category_id);
				}

				// System.out.println(columName);
				Map map = new HashMap();
				map.put("titile", categoryColums.get(i));
				if (i == columSeq - 1) {
					if ("特别推送".equals(columName)) {
						map.put("courselist", specialPushCourses);
						map.put("articlelist", null);
					} else if ("精品微课".equals(columName)) {
						map.put("courselist", competitiveCourses);
						map.put("articlelist", null);
					} else if ("中央精神".equals(columName)) {
						map.put("courselist", null);
						map.put("articlelist", centralSpirits);
					} else if ("地方动态".equals(columName)) {
						map.put("courselist", null);
						map.put("articlelist", localDynamics);
					} else if ("专家解读".equals(columName)) {
						map.put("courselist", null);
						map.put("articlelist", expertInterpretations);
					} else {
						map.put("courselist", null);
						map.put("articlelist", hotWords);
					}
				}

				resultList.add(i, map);

			}

			// data.put("specialPushCourses", specialPushCourses);
			// data.put("competitiveCourses", competitiveCourses);
			//
			// data.put("centralSpirits", centralSpirits);
			// data.put("localDynamics", localDynamics);
			// data.put("expertInterpretations", expertInterpretations);
			// data.put("hotWords", hotWords);

			data.put("resultList", resultList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 动态化的首页下部接口
	 * 
	 * @author zhang.xx
	 * @date 2016年12月3日13:19:07
	 */
	public void topBottom() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		List resultList = new ArrayList();

		try {

			// 预留的学员ID参数，特别推送应该会用到；
			Long studentId = getParaToLong("studentId");

			/**
			 * 列表分类： （1）特别推送（最新的推送的课程4个）； （2）精品微课（最新课程4门）；
			 * （3）中央精神；（4）地方动态；（5）专家解读；（6）热点词条； 3、4是资讯，5、6是文章，但是后台都是用的article表；
			 */

			// （1）特别推送 推送未做，暂时使用模拟数据
			List<PushStudent> pStudents = PushStudent.dao.getPushStuListByStuId(studentId);
			List pushIdList = new ArrayList();

			List<Push> pushList = new ArrayList<Push>();
			if (pStudents != null && pStudents.size() > 0) {
				for (int i = 0; i < pStudents.size(); i++) {
					long pushId = pStudents.get(i).getLong("push_id");

					Push push = Push.dao.findById(pushId);
					if (push != null) {
						// 从共享获取推送图片
						String pushImg = CommonUtil.getPushimgUrl(push);
						push.set("img", pushImg);

						pushList.add(push);
						if (pushList.size() >= 2) {
							// 只取两条推送记录
							break;
						}
					}
					// pushIdList.add(pushId);
				}
			}
			// 如上：首页修改为获取2个推送的列表，而不是返回4门推送的课程；

			// 如下：添加推送的全部推送标志is_all为1时候，给新添加的学员也关联这个推送；
			if (pushList.size() < 2) {
				List<Push> isAllPushs = Push.dao.getIsAllPushList();
				for (Push p : isAllPushs) {
					if (!pushList.contains(p)) {
						// 从共享获取推送图片
						String pushImg = CommonUtil.getPushimgUrl(p);
						p.set("img", pushImg);

						pushList.add(p);
						if (pushList.size() >= 2) {
							// 只取两条推送记录
							break;
						}
					}
				}
			}

			/*
			 * List<CourseBase> courseBases = new ArrayList<CourseBase>(); if
			 * (pushIdList.size() > 0) { for (int i = 0; i < pushIdList.size();
			 * i++) { long pushId = (Long) pushIdList.get(i); Push push =
			 * Push.dao.findById(pushId);
			 * 
			 * String courseIds = push.getStr("course_ids"); if (courseIds !=
			 * null && !"".equals(courseIds)) {
			 * 
			 * String[] coursesIdArr = courseIds.split(","); for (int j = 0; j <
			 * coursesIdArr.length; j++) { // 修改为根据course_no 2016年12月6日13:32:00
			 * // CourseBase courseBase = //
			 * CourseBase.dao.findById(coursesIdArr[j]); CourseBase courseBase =
			 * CourseBase.dao.findByCourseNo(coursesIdArr[j]); if (courseBase !=
			 * null) { courseBases.add(courseBase); } } if (courseBases.size() >
			 * 4) { // 截取前4个 courseBases = courseBases.subList(0, 4); break; } }
			 * 
			 * } } List<CenterCourseBase> specialPushCourses =
			 * DatahandleUtil.handleCourseList(courseBases);
			 */

			// （2）精品微课
			List<CourseBase> competitiveCourse = CourseBase.dao.getFourCompetitiveCourses();
			List<CenterCourseBase> competitiveCourses = DatahandleUtil.handleCourseList(competitiveCourse);
			// （2）精品微课
			List<CourseBase> fumeitiCourse = CourseBase.dao.getFourRichCourses();
			List<CenterCourseBase> fumeitiCourses = DatahandleUtil.handleCourseList(fumeitiCourse);

			long categoryId = 1;
			int articleType = 0;
			// 与上面的分类列表联动，顺序一致；
			List<CategoryColum> categoryColums = CategoryColum.dao.getCategoryColums();
			for (int i = 0; i < categoryColums.size(); i++) {
				long columId = categoryColums.get(i).getLong("id");
				String columName = categoryColums.get(i).getStr("name");
				int columSeq = categoryColums.get(i).getInt("seq");

				// 根据分类名称查询相应的分类ID
				Long category_id;
				ArticleCategory ac = ArticleCategory.dao.getArticleCategoryByName(columName);
				if (ac != null) {
					category_id = ac.getLong("id");
					categoryColums.get(i).set("category_id", category_id);
					// 补全图片路径
					String img = CommonUtil.getCategoryColumimgUrl(categoryColums.get(i));
					categoryColums.get(i).set("img", img);

				}

				// System.out.println(columName);
				Map map = new HashMap();
				map.put("titile", categoryColums.get(i));
				// 前两个是固定板块
				// 也修改为不固定?
				if (i == 0) {
					// 精品微课
					map.put("courselist", competitiveCourses);
					map.put("articlelist", null);
					map.put("pushList", null);
				} else if (i == 1) {
					// 富媒体 add 2017年3月8日15:33:45
					
					map.put("courselist", fumeitiCourses);
					map.put("articlelist", null);
					map.put("pushList", null);
				} else if (i == 2) {
					// 特别推送
					map.put("pushList", pushList);
					// map.put("courselist", specialPushCourses);
					map.put("courselist", null);
					map.put("articlelist", null);
				} else {
					// 之后的为动态的资讯文章板块数量不定；
					List<Article> articles = new ArrayList<Article>();

					categoryId = categoryColums.get(i).getLong("category_id");
					articleType = categoryColums.get(i).getInt("type");
					int limit = 3;
					if (articleType == 0) {
						limit = 2;
					} else if (articleType == 2) {
						limit = 4;
					}
					articles = Article.dao.getFourFirstPageArticles(articleType, categoryId, limit);

					// 对资讯list进行图片处理
					for (int j = 0; j < articles.size(); j++) {
						Article a = articles.get(j);
						String img = a.getStr("img");
						if (img != null && !"".equals(img)) {
							img = CommonUtil.getArticleHeadUrl(img);
						}
						a.set("img", img);
					}

					map.put("pushList", null);
					map.put("courselist", null);
					map.put("articlelist", articles);
				}

				resultList.add(i, map);

			}

			data.put("resultList", resultList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 标签列表接口
	 * 
	 * @date 2016年11月12日09:27:38
	 */
	public void tagList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			// Long courseId = getParaToLong("courseId");
			// Long studentId = getParaToLong("studentId");
			// int pageNumber = getParaToInt("pageNumber");
			// int pageSize = getParaToInt("pageSize");

			List<Tag> tagList = Tag.dao.getTagList();

			data.put("tagList", tagList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 定制喜好标签
	 * 
	 * 2016年11月12日09:54:46
	 */
	public void addTags() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");
			String tagIds = getPara("tagIds"); // 逗号分隔

			Student student = Student.dao.findById(studentId);
			if (!"".equals(tagIds)) {
				String[] tagArr = tagIds.split(",");

				// 遍历标签
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < tagArr.length; i++) {
					// 拼接标签ID
					sb.append(tagArr[i]);
					if (i != tagArr.length - 1) {
						sb.append(",");
					}
				}
				// update学员标签
				student.set("tags", sb.toString());
				student.update();

			}

			dataMap.put("error_code", "0");
			dataMap.put("msg", "成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 猜你喜欢二级列表
	 * 
	 * @date 2016年11月14日13:51:11 第一版本
	 */
	public void guessYouLikeList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");

			Page<CourseBase> guessLikePage = CourseBase.dao.getGuessLikeCoursesPage(pageNumber, pageSize);
			List<CourseBase> guessLikeCourse = guessLikePage.getList();

			// 强化列表的返回类
			List<CenterCourseBase> guessLikeCourses = DatahandleUtil.handleCourseList(guessLikeCourse);

			data.put("guessLikeCourses", guessLikeCourses);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}
	
	/**
	 * 富媒体课程二级列表 
	 * 
	 * @date 2017年3月8日16:38:28
	 */
	public void richCourseList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();
		
		try {
			
			Long studentId = getParaToLong("studentId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");
			
			Page<CourseBase> guessLikePage = CourseBase.dao.getRichCoursesPage(pageNumber, pageSize);
			List<CourseBase> guessLikeCourse = guessLikePage.getList();
			
			// 强化列表的返回类
			List<CenterCourseBase> guessLikeCourses = DatahandleUtil.handleCourseList(guessLikeCourse);
			
			data.put("richCourses", guessLikeCourses);
			
			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);
			
		} catch (Exception e) {
			e.printStackTrace();
			
			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}
		
		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 资讯列表
	 * 
	 * @date 2016年11月13日17:01:22
	 */
	public void articleList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long articleType = getParaToLong("articleType");
			Long categoryId = getParaToLong("categoryId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");

			/*
			 * (1)最新：7天; (2)本月：30天; (3)全部
			 * 
			 * 修改： (1)最新：时间倒序; (2)最热：点赞数; (3)综合，前面两个 都用
			 */
			int dateType = getParaToInt("type");

			StringBuffer sql = new StringBuffer();
			sql.append(" from t_article t WHERE 1=1 and t.category_id = ? AND t.article_type = ?  ");
			// sql.append(" from t_article t WHERE 1=1 and t.category_id = ? ");
			if (dateType == 1) {
				// 7天
				// sql.append(" and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <=DATE(t.create_time) ");
				// 最新
				sql.append(" ORDER BY t.create_time DESC ");
			} else if (dateType == 2) {
				// 30天
				// sql.append(" and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <=DATE(t.create_time) ");
				// 最热
				sql.append(" ORDER BY t.ckgood_num desc ");
			} else {
				// 全部
				// 综合
				sql.append(" ORDER BY t.browse desc,t.ckgood_num desc ");
			}

			// sql.append(" ORDER BY t.create_time DESC ");

			// 分页查询
			Page<Article> articlePage = Article.dao
					.getArticlesPage(pageNumber, pageSize, articleType, categoryId, sql.toString());
			List<Article> articleList = articlePage.getList();

			for (int i = 0; i < articleList.size(); i++) {
				Article a = articleList.get(i);
				String img = a.getStr("img");
				if (img != null && !"".equals(img)) {
					img = CommonUtil.getArticleHeadUrl(img);
				}
				a.set("img", img);
			}

			data.put("articleList", articleList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 资讯详情
	 * 
	 * @date 2016年11月13日17:18:16 简陋版本，缺失好多东西；
	 * @date 2016年11月14日15:09:29
	 */
	public void articleDetail() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long articleId = getParaToLong("articleId");
			Long studentId = getParaToLong("studentId");

			Article article = Article.dao.findById(articleId);

			// (1)相关资讯
			List<Article> aboutArticles = new ArrayList<Article>();
			// (2)相关微课
			List<CourseBase> aboutCourses = new ArrayList<CourseBase>();

			if (article != null) {
				// 获取课程的关键词
				String keyWords = article.getStr("key_words");

				if (keyWords != null && !"".equals(keyWords)) {
					String[] keyWordsArr = keyWords.split("&");

					// 遍历课程关键词
					// （a）相关资讯的遍历
					for (int i = 0; i < keyWordsArr.length; i++) {
						String keyWord = keyWordsArr[i];

						// 使用like查询
						// 只查询属于APP分类（即类别板块）下的Article；2017年2月8日
						// 具体操作为在getFourAboutArticlesWithoutOwn的sql中添加文章分类的关联，并添加column_id<>0的条件来进行限制；
						List likeArticleList = Article.dao.getFourAboutArticlesWithoutOwn(keyWord, articleId);

						aboutArticles.addAll(likeArticleList);

						if (aboutArticles.size() >= 4) {
							// 截取前4个
							aboutArticles = aboutArticles.subList(0, 4);
							break;
						}

					}
					// （b）相关课程的遍历
					for (int i = 0; i < keyWordsArr.length; i++) {
						String keyWord = keyWordsArr[i];

						// 使用like查询
						List likeCourseList = CourseBase.dao.getFourAboutCourses(keyWord);

						aboutCourses.addAll(likeCourseList);

						if (aboutCourses.size() >= 12) {
							// 截取前4个
							aboutCourses = aboutCourses.subList(0, 12);
							break;
						}

					}
				}

			}

			// 浏览量递增
			article.set("browse", article.getLong("browse") + 1);
			article.update();

			// 获取点赞状态
			if (ArticleStudentStatus.dao.getArticleStudentStatus(articleId, studentId, "1") != null) {
				data.put("like", 1);
			} else {
				data.put("like", 0);
			}
			if (ArticleStudentStatus.dao.getArticleStudentStatus(articleId, studentId, "2") != null) {
				data.put("collect", 1);
			} else {
				data.put("collect", 0);
			}
			
			// 处理资讯信息，包括资讯的图片和资讯分类图片 2017年1月19日14:36:51
			for (int i = 0; i < aboutArticles.size(); i++) {
				// 补全图片路径
				Article a = aboutArticles.get(i);
				String img = a.getStr("img");
				if (img != null && !"".equals(img)) {
					img = CommonUtil.getArticleHeadUrl(img);
				}
				a.set("img", img);
				
				ArticleCategory ac = ArticleCategory.dao.findById(a.get("category_id"));
				String categoryName = ac.getStr("category_name");

				CategoryColum cc = CategoryColum.dao.getCategoryColumByName(categoryName);
				String cimg = CommonUtil.getCategoryColumimgUrl(cc);

				a.set("column_img", cimg);
			}

			data.put("article", article);
			data.put("aboutArticles", aboutArticles);
			
			// 修改相关微课返回格式与课程详情的相关微课一致；
			List aboutCourseList = new ArrayList();
			for (CourseBase cb : aboutCourses) {
				cb.set("url", CommonUtil.getMediaUrl(cb));
				cb.set("small_img", CommonUtil.getsmallImgUrl(cb));
				cb.set("large_img", CommonUtil.getbigImgUrl(cb));

				Map<String, Object> aboutDataMap = new HashMap<String, Object>();
				aboutDataMap.put("courseBase", cb);
				aboutDataMap.put("courseLesson", cb.getCourseLesson());
				aboutDataMap.put("courseLearningSetting", cb.getCourseLearningSetting());
				aboutCourseList.add(aboutDataMap);
			}

			data.put("aboutCourses", aboutCourseList);
			// data.put("aboutCourses", aboutCourses);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 相关资讯
	 * 
	 * 合并到课程详情接口，此接口仍旧保留； 2016年11月30日10:22:29
	 * 
	 * @date 2016年11月14日14:12:00 begin
	 */
	public void aboutArticle() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long courseId = getParaToLong("courseId");
			// Long studentId = getParaToLong("studentId");
			// int pageNumber = getParaToInt("pageNumber");
			// int pageSize = getParaToInt("pageSize");

			List<Article> aboutArticles = new ArrayList<Article>();

			CourseBase courseBase = CourseBase.dao.findById(courseId);
			if (courseBase != null) {
				// 获取课程的关键词
				// 更换为之前的关键词字段
				String keyWords = courseBase.getStr("tags");

				if (keyWords != null && !"".equals(keyWords)) {
					String[] keyWordsArr = keyWords.split("&");

					// 遍历课程关键词
					for (int i = 0; i < keyWordsArr.length; i++) {
						String keyWord = keyWordsArr[i];

						// 使用like查询
						List likeArticleList = Article.dao.getFourAboutArticles(keyWord);

						aboutArticles.addAll(likeArticleList);

						if (aboutArticles.size() >= 4) {
							// 截取前4个
							aboutArticles = aboutArticles.subList(0, 4);
							break;
						}

					}
				}

			}

			data.put("aboutArticles", aboutArticles);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 学员推送列表接口
	 * 
	 * @author zhang.xx
	 * @date 2016年12月8日10:58:51
	 */
	public void pushList() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");

			// 查询学员的推送列表
			List<PushStudent> pushStudentList = PushStudent.dao.getPushStuListByStuId(studentId);
			List<Push> pushList = new ArrayList();
			for (int i = 0; i < pushStudentList.size(); i++) {
				long pushId = pushStudentList.get(i).getLong("push_id");

				Push push = Push.dao.findById(pushId);
				// 添加null判断； 2016年12月27日09:42:15
				if (push != null) {

					// 从共享获取推送图片
					String pushImg = CommonUtil.getPushimgUrl(push);
					push.set("img", pushImg);

					pushList.add(push);
				}
			}

			// 处理推送列表中is_all为1的推送，给新添加的学员也添加这个推送； 2016年12月12日15:52:51
			List<Push> isAllPushs = Push.dao.getIsAllPushList();
			for (Push p : isAllPushs) {
				if (!pushList.contains(p)) {

					// 从共享获取推送图片
					String pushImg = CommonUtil.getPushimgUrl(p);
					p.set("img", pushImg);

					pushList.add(p);
				}
			}

			data.put("pushList", pushList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 推送详情接口
	 * 
	 * @date 2016年12月8日13:38:19
	 */
	public void pushDetail() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");
			// 每次只取一个推送，用下面的pageNumber来标记；2016年11月17日09:46:30
			// int pageNumber = getParaToInt("pageNumber");
			// 更改为使用pushId获取单个推送的详情； 2016年12月8日13:33:48
			long pushId = getParaToLong("pushId");

			PushVO pushVO = new PushVO();

			// 查询学员的推送列表
			// List<PushStudent> pushStudentList =
			// PushStudent.dao.getPushStuListByStuId(studentId);
			// List pushIdList = new ArrayList();
			// for (int i = 0; i < pushStudentList.size(); i++) {
			// long pushId = pushStudentList.get(i).getLong("push_id");
			// pushIdList.add(pushId);
			// }

			// if (pushIdList.size() > pageNumber - 1) {
			// } else {
			// pushVO = null;
			// }

			Push push = Push.dao.findById(pushId);

			// 从共享获取推送图片
			String pushImg = CommonUtil.getPushimgUrl(push);
			push.set("img", pushImg);

			List<CenterCourseBase> courses = new ArrayList<CenterCourseBase>();

			String courseIds = push.getStr("course_ids");
			if (courseIds != null && !"".equals(courseIds)) {

				List<CourseBase> courseBases = new ArrayList<CourseBase>();
				String[] coursesIdArr = courseIds.split(",");
				for (int j = 0; j < coursesIdArr.length; j++) {
					// CourseBase courseBase =
					// CourseBase.dao.findById(coursesIdArr[j]);
					CourseBase courseBase = CourseBase.dao.findByCourseNo(coursesIdArr[j]);

					courseBases.add(courseBase);
				}
				if (courseBases.size() > 0) {
					courses = DatahandleUtil.handleCourseList(courseBases);
				}
			}

			pushVO.setPush(push);
			pushVO.setCourses(courses);

			data.put("pushVO", pushVO);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 个人中心接口
	 * 
	 * @date 2016年11月18日09:32:43
	 */
	public void personCenter() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");

			Student student = Student.dao.findById(studentId);
			String tags = student.get("tags");

			List<Tag> tagList = new ArrayList<Tag>();
			if (tags != null && !"".equals(tags)) {
				String[] tagsArr = tags.split(",");

				for (int i = 0; i < tagsArr.length; i++) {
					String tagId = tagsArr[i];

					Tag tag = Tag.dao.findById(tagId);
					tagList.add(tag);
				}

			}

			data.put("tagList", tagList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 资讯文章点赞
	 * 
	 * 重构为资讯文章操作接口，包括点赞和收藏功能；
	 * 
	 * @date 2016年11月22日10:17:28
	 * @modified 2016年12月21日14:38:57
	 */
	public void ckgoodArticle() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			Long studentId = getParaToLong("studentId");
			Long articleId = getParaToLong("articleId");
			String type = getPara("type");

			Article article = Article.dao.findById(articleId);
			/*
			 * if (article != null) { article.set("ckgood_num",
			 * article.getInt("ckgood_num") != null ?
			 * article.getInt("ckgood_num") + 1 : 0); article.update(); }
			 */
			// String type = "1";
			if ("1".equals(type)) {
				// 资讯的点赞
				ArticleStudentStatus articleStudentStatus = getModel(ArticleStudentStatus.class);
				if (ArticleStudentStatus.dao.getArticleStudentStatus(articleId, studentId, type) == null) {
					article.set("ckgood_num", article.getInt("ckgood_num") != null ? article.getInt("ckgood_num") + 1 : 0);
					article.update();

					ArticleStudentStatus.dao.save(articleStudentStatus, articleId, studentId, type);

					int ckGoodNum = article.getInt("ckgood_num");
					data.put("ckGoodNum", ckGoodNum);

					dataMap.put("error_code", "0");
					dataMap.put("msg", "点赞成功！");
					dataMap.put("data", data);
				} else {
					dataMap.put("error_code", "1");
					dataMap.put("msg", "已点过赞！");
					dataMap.put("data", data);
				}
			} else if ("2".equals(type)) {
				// 资讯的收藏
				ArticleStudentStatus articleStudentStatus = getModel(ArticleStudentStatus.class);
				if (ArticleStudentStatus.dao.getArticleStudentStatus(articleId, studentId, type) == null) {

					article.set("collection_num", article.getInt("collection_num") != null ? article.getInt("collection_num") + 1
							: 0);
					article.update();

					ArticleStudentStatus.dao.save(articleStudentStatus, articleId, studentId, type);

					// int ckGoodNum = article.getInt("ckgood_num");
					// data.put("ckGoodNum", ckGoodNum);

					dataMap.put("error_code", "0");
					dataMap.put("msg", "收藏成功！");
					dataMap.put("data", data);
				} else {
					dataMap.put("error_code", "1");
					dataMap.put("msg", "已收藏！");
					dataMap.put("data", data);
				}

			}

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "点赞失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 资讯关键字搜索接口
	 * 
	 * @author zhang.xx
	 * @date 2016年11月26日15:28:28
	 */
	@Before(ArticleKeyCondition.class)
	public void articleSeatch() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			// Long studentId = getParaToLong("studentId");

			Long categoryId = getParaToLong("categoryId");
			int pageNumber = getParaToInt("pageNumber");
			int pageSize = getParaToInt("pageSize");

			StringBuilder sql = new StringBuilder();
			List<Object> paras = new ArrayList<Object>();
			kcd.buildSql(sql, paras);

			Page<Article> articlePage = Article.dao.getPageLikeQueryString(pageNumber, pageSize, sql.toString(), paras.toArray());
			List<Article> articleList = articlePage.getList();

			// 进行图片的补全； 2016年12月13日15:45:59
			for (int i = 0; i < articleList.size(); i++) {
				Article a = articleList.get(i);
				String img = a.getStr("img");
				if (img != null && !"".equals(img)) {
					img = CommonUtil.getArticleHeadUrl(img);
				}
				a.set("img", img);
			}

			data.put("articleList", articleList);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	/**
	 * 闪屏页图片接口
	 * 
	 * @date 2016年11月30日10:49:12
	 */
	public void welcomPages() {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		Map<String, Object> data = new HashMap<String, Object>();

		try {

			List<WelcomPages> welcomPages = WelcomPages.dao.getWelcomPages();

			for (int i = 0; i < welcomPages.size(); i++) {
				// 补全路径
				WelcomPages w = welcomPages.get(i);
				String img = CommonUtil.getWelcomeimgUrl(w);
				w.set("img", img);
			}

			data.put("welcomPages", welcomPages);

			dataMap.put("error_code", "0");
			dataMap.put("msg", "获取成功！");
			dataMap.put("data", data);

		} catch (Exception e) {
			e.printStackTrace();

			dataMap.put("error_code", "1");
			dataMap.put("msg", "获取失败！");
			dataMap.put("data", data);
		}

		renderJson(JsonKit.mapToJson(dataMap, 100));
	}

	// for (int i = 0; i < pushStudentList.size(); i++) {
	// long pushId = pushStudentList.get(i).getLong("push_id");
	//
	// PushVO pushVo = new PushVO();
	// Push push = Push.dao.findById(pushId);
	// List<CenterCourseBase> courses = new ArrayList<CenterCourseBase>();
	//
	// String courseIds = push.getStr("course_ids");
	// if (courseIds != null && !"".equals(courseIds)) {
	//
	// List<CourseBase> courseBases = new ArrayList<CourseBase>();
	// String[] coursesIdArr = courseIds.split(",");
	// for (int j = 0; j < coursesIdArr.length; j++) {
	// CourseBase courseBase = CourseBase.dao.findById(coursesIdArr[i]);
	//
	// courseBases.add(courseBase);
	// }
	//
	// if (courseBases.size() > 0) {
	// courses = DatahandleUtil.handleCourseList(courseBases);
	// }
	//
	// }
	//
	// pushVo.setPush(push);
	// pushVo.setCourses(courses);
	//
	// pushList.add(pushVo);
	// }
}
